package ytdp.careagle.com.ytdp.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.hardware.Camera;
import android.location.LocationManager;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;

import java.math.BigDecimal;
import java.text.DecimalFormat;

import ytdp.careagle.com.ytdp.app.G;


/**
 * create by Weavey
 * on date 2016-01-06
 * TODO
 */

public class Utils {

    public static boolean isOtherPhotoId(int photoId) {
        if (photoId >= G.startOtherPhotoId && photoId <= G.endOtherPhotoId) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断GPS是否开启，GPS或者AGPS开启一个就认为是开启的
     *
     * @param context
     * @return true 表示开启
     */
    public static final boolean isOPen(final Context context) {
        LocationManager locationManager
                = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (gps || network) {
            return true;
        }

        return false;
    }

    public static String formatInt(String strDouble) {
        try {
            BigDecimal bg = new BigDecimal(strDouble);
            strDouble = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
            String[] split = strDouble.split("\\.");
            String strPoint = split[1];
            Long aDouble = Long.valueOf(strPoint);
            if (aDouble == 0 || split[0].length() > 4) {
                return split[0];
            } else {
                return strDouble;
            }
        } catch (Exception e) {
            return strDouble;
        }
    }

    public static String format2Right(String strDouble) {
        BigDecimal bg = new BigDecimal(strDouble);
        strDouble = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "";
        return strDouble;

    }

    public static Drawable getDrawble(Context conetxt, @DrawableRes int id) {
        return ContextCompat.getDrawable(conetxt, id);
    }

    public static int getColor(Context conetxt, @ColorRes int id) {
        return ContextCompat.getColor(conetxt, id);
    }

    public static String getString(Context conetxt, @StringRes int id) {
        return conetxt.getResources().getString(id);
    }

    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics()
                .scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    public static int dp2px(Context context, int dip) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

    public static <T extends View> T findViewById(View v, int id) {
        return (T) v.findViewById(id);
    }

    public static String formatString(float data) {
        DecimalFormat df = new DecimalFormat("#,###");
        return df.format(data);
    }

    public static String formatString(int data) {
        DecimalFormat df = new DecimalFormat("#,###");
        return df.format(data);
    }

    public static String formatString(double data) {
        DecimalFormat df = new DecimalFormat("#,###");
        return df.format(data);
    }

    public static String formatString(String data) {
        DecimalFormat df = new DecimalFormat("#,###");
        return df.format(data);
    }

    public static boolean isCameraCanUse() {
        boolean canUse = true;
        Camera mCamera = null;
        try {
            // TODO camera驱动挂掉,处理??
            mCamera = Camera.open(G.CAMERA_FRONT_OR_BACK);
        } catch (Exception e) {
            canUse = false;
        }
        if (canUse) {
            mCamera.release();
            mCamera = null;
        }

        return canUse;
    }

    public static String NumberFormat(float f, int m) {
        return String.format("%." + m + "f", f);
    }

    public static float NumberFormatFloat(float f, int m) {
        String strfloat = NumberFormat(f, m);
        return Float.parseFloat(strfloat);
    }

    //传入屏幕分辨率的像素高 自动生成尺寸 用debug调试可以直接获取
    public static String takeHeightXmlDimens(float height) {
        Log.e("tag", G.height + "--" + G.width);
        double scale = height / 400;
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 1; i <= 400; i++) {
            double px = i * scale;
            stringBuffer.append("<dimen name=\"y" + i + "\">" + Utils.formatInt(px + "") + "px</dimen>");
        }
        return new String(stringBuffer);
    }

    //传入屏幕分辨率的像素高 自动生成尺寸 用debug调试可以直接获取
    public static String takeWithXmlDimens(float with) {
        Log.e("tag", G.height + "--" + G.width);
        double scale = with / 320;
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 1; i <= 400; i++) {
            double px = i * scale;
            stringBuffer.append("<dimen name=\"y" + i + "\">" + Utils.formatInt(px + "") + "px</dimen>");
        }
        return new String(stringBuffer);
    }
}